package com.nation.controller;

import com.nation.domain.TImportantResearchWork;
import com.nation.domain.User;
import com.nation.serviceImpl.ResearchWorkServiceImpl;
import com.nation.utils.ReadAndStoreFile;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by YeziAny on 2017/11/25.
 */
@Controller
@RequestMapping(value = "/researchWork")
public class ResearchWorkController {
    @Autowired
    ResearchWorkServiceImpl researchWorkServiceImpl;
    @Autowired
    com.nation.service.NationInfoService NationInfoService;

    /*
   * 上传重要文献研究著作
   * @param*/
    @RequestMapping(value="uploadResearchWorks.do",method = RequestMethod.POST)
    @ResponseBody
    public String uploadResearchWorks(HttpServletRequest request, @RequestParam(value = "nation_z")String nation_z,@RequestParam(value = "title")String title,
                                      @RequestParam(value = "author")String author,@RequestParam(value = "publisher")String publisher,
                                      @RequestParam(value = "publish_time")String publish_time,@RequestParam(value = "e_book",required=false)MultipartFile e_book){
        org.apache.shiro.subject.Subject currentuser= SecurityUtils.getSubject();
        User user=(User)currentuser.getPrincipal();
        String input_code=user.getYHZH();
        TImportantResearchWork tImportantResearchWork=new TImportantResearchWork(title,author,publisher,publish_time);
        tImportantResearchWork.setRecoderId(input_code);
        try {
            String path = request.getSession().getServletContext().getResource("/").getPath();
            path += ReadAndStoreFile.TImportantResearchWork;

            System.out.println("path--" + path);

//          将MultipartFile格式转换成File
            String file_path = ReadAndStoreFile.save(e_book, path);
            tImportantResearchWork.seteBook(file_path);
            String country_code = NationInfoService.getCountryCodeByNation_z(nation_z);
            tImportantResearchWork.setCountryCode(country_code);

            researchWorkServiceImpl.uploadResearchWorks(tImportantResearchWork);

        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }
        return "上传成功";

    }
//    public String uploadResearchWorks(@RequestParam(value = "nation_z")String naiton_z,@RequestParam(value = "title")String title,
//                                      @RequestParam(value = "author")String author,@RequestParam(value = "publisher")String publisher,
//                                      @RequestParam(value = "publish_time")String publish_time,@RequestParam(value = "e_book",required=false)MultipartFile e_book){
//
////
////    }


}
